package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :Arrow
	CreateTime:2009-05-12 09:11:45
*/
public class BillMoneyInputSrv extends IHandler
{

	public BillMoneyInputSrv() 
	{
		super();
	}
	private void deleteBillMoneyInput(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		BillMoneyInput obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = BillMoneyInput.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveBillMoneyInput(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		BillMoneyInput obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = BillMoneyInput.fetch();
    	}
    	else                     //update
    	{
    		obj = BillMoneyInput.fetch(con,id);
    	}
		BillMoneyInputPageData data = (BillMoneyInputPageData)PageData.fromForm(context,BillMoneyInputPageData.class);
		data.setCreateUserId(loginContext.getLoginUserId());
		data.setCreateUser(loginContext.getLoginUserName());
		data.setCreateTime(new DateUtil().getFullString());
		
		String warehouseCode = data.getWarehouseCode();
		Warehouse warehouse = Warehouse.fetchByCode(con, warehouseCode);
		if(warehouse == null) warehouse = Warehouse.fetch();
		data.setWarehouseName(warehouse.getName());
		context.setValue("BillMoneyInputPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listBillMoneyInput(DBConnection con,SrvContext context, ILoginContext loginContext, String path) throws Exception
	{
	    BillMoneyInputSearchEvent event = (BillMoneyInputSearchEvent)SearchEvent.fromForm(context,BillMoneyInputSearchEvent.class);
	    if("".equals(event.getWarehouseCode())){
            event.setWarehouseCode(loginContext.getLoginWarehouseCode());
        }
	    context.setSessionValue("BillMoneyInputSearchEvent",event);
		context.setNextUrl(path+"BillMoneyInputList.jsp");
	}
	private void backBillMoneyInput(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"BillMoneyInputList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listBillMoneyInput"))
		{
			listBillMoneyInput(con,context,loginContext, path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewBillMoneyInput"))
		{
			String id = context.getString("Id",null);
			BillMoneyInput obj = BillMoneyInput.fetch(con,id);
			context.setValue("BillMoneyInput",obj);
			context.setNextUrl(path+"BillMoneyInputView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteBillMoneyInput"))
		{
			context.setErrorUrl(path+"BillMoneyInputList.jsp");
			deleteBillMoneyInput(con,context);
			backBillMoneyInput(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddBillMoneyInput"))
		{
			BillMoneyInput obj = BillMoneyInput.fetch();
			BillMoneyInputPageData data = (BillMoneyInputPageData)PageData.fromTable(obj,BillMoneyInputPageData.class);
			context.setValue("BillMoneyInputPageData",data);
			context.setNextUrl(path+"BillMoneyInputEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateBillMoneyInput"))
		{
			String id = context.getString("Id",null);
			BillMoneyInput obj = BillMoneyInput.fetch(con,id);
			BillMoneyInputPageData data = (BillMoneyInputPageData)PageData.fromTable(obj,BillMoneyInputPageData.class);
			context.setValue("BillMoneyInputPageData",data);
			context.setNextUrl(path+"BillMoneyInputEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyBillMoneyInput"))
		{
			String id = context.getString("Id",null);
			BillMoneyInput obj = BillMoneyInput.copy(BillMoneyInput.fetch(con,id));
			BillMoneyInputPageData data = (BillMoneyInputPageData)PageData.fromTable(obj,BillMoneyInputPageData.class);
			context.setValue("BillMoneyInputPageData",data);
			context.setNextUrl(path+"BillMoneyInputEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveBillMoneyInput"))
		{
			context.setErrorUrl(path+"BillMoneyInputEdit.jsp");
			saveBillMoneyInput(con,context,loginContext);
			backBillMoneyInput(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewBillMoneyInput"))
		{
			context.setErrorUrl(path+"BillMoneyInputEdit.jsp");
			saveBillMoneyInput(con,context,loginContext);
			BillMoneyInput obj = BillMoneyInput.fetch();
			BillMoneyInputPageData data = (BillMoneyInputPageData)PageData.fromTable(obj,BillMoneyInputPageData.class);
			context.setValue("BillMoneyInputPageData",data);
			context.setNextUrl(path+"BillMoneyInputEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListBillMoneyInput"))
		{
			backBillMoneyInput(con,context,path);
			return;
		}
		
		//add by Arrow start

		//add by Arrow end

		Logger.throwException("BillMoneyInputSrv","unknown action_code="+action_code);
	}
}
